﻿/*
 * Created by SharpDevelop.
 * User: cszhangyi
 * Date: 2016/9/7
 * Time: 16:36
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using Fiddler;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using System.Web;
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BuriedPointsForYMT
{
	/// <summary>
	/// Description of AutoTamperReqBeforeHandler.
	/// </summary>
	public class AutoTamperReqBeforeHandler
	{	
		private Session oSession;
		private BuriedPointsTabPage oPage;
		private string response_str = "";
		
		public AutoTamperReqBeforeHandler(Session oSession, BuriedPointsTabPage oPage)
		{
			this.oSession = oSession;
            this.oPage = oPage;
		}
		
		public void udpateSessionList() 
		{
			int rowNum = int.Parse(oPage.get_PageType_textbox().Text) - 2;
			string fileName = oPage.get_model_name_textbox().Text;
			DataSet data = ExcelTool.LoadDataFromExcel(fileName);
			
			string pageType = data.Tables[0].Rows[rowNum]["pagetype"].ToString();
			string moduleName = data.Tables[0].Rows[rowNum]["module_name"].ToString();
			string subModuleName = data.Tables[0].Rows[rowNum]["sub_module_name"].ToString();
			string action_type = data.Tables[0].Rows[rowNum]["action_type"].ToString();
			string biz_param = data.Tables[0].Rows[rowNum]["biz_param"].ToString();
			bool isBizFromExcel = true;
			bool isbizFromReq = true;
			string[] paramArray;

			string json_str = String.Join("", oSession.GetRequestBodyAsString());
			json_str = HttpUtility.UrlDecode(json_str);
			
			if (json_str == "")
				return;
			
			JArray ja =(JArray) JsonConvert.DeserializeObject(json_str);
			
			foreach(JObject obj in ja)
			{
				if (biz_param == "") {
					isBizFromExcel = false;
					isbizFromReq = false;
				} else {
					isBizFromExcel = true;
					isbizFromReq = true;
				}
				
				if ((Object)obj["module_name"] == null)
					obj["module_name"] = "";
				
				if ((Object)obj["sub_module_name"] == null)
					obj["sub_module_name"] = "";
				
				if ((Object)obj["pagetype"] == null)
					obj["pagetype"] = "";
				
				if ((Object)obj["action"] == null)
					obj["action"] = "";
				
				if (isBizFromExcel) {
					paramArray = biz_param.Split(new char[2]{'，', ','});
					
					for (int i = 0; i < paramArray.Length; i++) {
						if ((Object)obj[paramArray[i]] == null)
						{
							obj[paramArray[i]] = "";
							isbizFromReq = false;
						}
						//FiddlerApplication.Log.LogString(obj[paramArray[i]].ToString());
					}
				}
				
				/*FiddlerApplication.Log.LogString(obj["pagetype"].ToString());
				FiddlerApplication.Log.LogString(pageType);
				FiddlerApplication.Log.LogString((pageType == obj["pagetype"].ToString()).ToString());
				
				FiddlerApplication.Log.LogString(obj["module_name"].ToString());
				FiddlerApplication.Log.LogString(moduleName);
				FiddlerApplication.Log.LogString((moduleName == obj["module_name"].ToString()).ToString());
				
				FiddlerApplication.Log.LogString(obj["sub_module_name"].ToString());
				FiddlerApplication.Log.LogString(subModuleName);
				FiddlerApplication.Log.LogString((subModuleName == obj["sub_module_name"].ToString()).ToString());
				
				FiddlerApplication.Log.LogString(obj["action"].ToString());
				FiddlerApplication.Log.LogString(action_type);
				FiddlerApplication.Log.LogString((action_type == obj["action"].ToString()).ToString());
				
				FiddlerApplication.Log.LogString((isbizFromReq.ToString()).ToString());*/
				
				if (pageType == obj["pagetype"].ToString() && moduleName == obj["module_name"].ToString() && 
				    subModuleName == obj["sub_module_name"].ToString() && action_type == obj["action"].ToString() && isbizFromReq)
				{
					this.oSession["ui-color"] = "purple";
					this.oSession["ui-bold"] = "true";
					FiddlerApplication.Log.LogString(JsonConvert.SerializeObject(obj));
					response_str = JsonConvert.SerializeObject(obj);
				}
				else
					continue;
			}
		}
		
		public string getResponseBody() {
			return response_str;
		}
	}
}
